﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System;

namespace DynamicGeometry
{
    public class LabelCreator : Behavior
    {
        [PropertyGridName("Text label")]
        public class Dialog
        {
            public Dialog(Label label)
            {
                this.label = label;
            }

            Label label;

            [PropertyGridVisible]
            [PropertyGridEvent("Loaded", "Loaded")]
            public string Text
            {
                get
                {
                    return label.Text;
                }
                set
                {
                    label.Text = value;
                }
            }

            public void Loaded(object sender, RoutedEventArgs e)
            {
                StringEditor editor = sender as StringEditor;
                editor.Label.Visibility = Visibility.Collapsed;
                editor.TextBox.Focus();
                editor.TextBox.SelectAll();
            }
        }

        public override void MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var label = Factory.CreateLabel(Drawing);
            label.Text = "Text";
            label.MoveTo(Coordinates(e));
            AddFigure(label, true);
            var drawing = Drawing;
            AbortAndSetDefaultTool();
            drawing.RaiseStatusNotification("");
            drawing.RaiseDisplayProperties(new Dialog(label));
        }

        public override string Name
        {
            get { return "Text"; }
        }

        public override string HintText
        {
            get
            {
                return "Click to add a text label.";
            }
        }

        public override FrameworkElement CreateIcon()
        {
            var text = new TextBlock()
            {
                Text = "Abc",
                FontStyle = FontStyles.Italic,
                FontWeight = FontWeights.Bold,
                HorizontalAlignment = HorizontalAlignment.Center,
                VerticalAlignment = VerticalAlignment.Center
            };
            var grid = new Grid()
            {
                MinWidth = Behavior.IconSize,
                MinHeight = Behavior.IconSize,
            };
            grid.Children.Add(text);
            return grid;
        }
    }
}